//
//  FPPostListViewController.m
//  ForestPack
//
//  Created by jimmy on 2018/7/6.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "FPPostListViewController.h"
#import "FPPostListTableViewCell.h"
#import "PostListModel.h"
#import "FPPostDetailModel.h"

#pragma mark - 公告列表控制器
@interface FPPostListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)  BaseTableView  * tableView;

@property (nonatomic,strong)  NSArray  * dataArray;
//@property (nonatomic,strong)  NSArray  * listArray;

@property (nonatomic,strong)  PostListModel  * postListModel;

@property (nonatomic,assign)  NSInteger       page;
@property (nonatomic,assign)  NSInteger       current_page;

@end

@implementation FPPostListViewController

- (NSArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [[NSArray alloc] init];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"公告列表"];
    [self setTableViewUI];
    
    [self refreshTableView];
    
}

-(void)refreshTableView
{
    WeakObj(self);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        selfWeak.current_page = 1;
        [selfWeak requestData];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        selfWeak.current_page ++;
        [selfWeak requestMoreData];
    }];
    [self.tableView.mj_header beginRefreshing];
}

-(void)requestData
{
    NSString *url = [NSString stringWithFormat:@"%@app/notice_record",REQUESTHEADER];
    NSDictionary *dict = @{@"page":@1};
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        NSDictionary * dict = successResponse;
        if (dict == nil) {
            return ;
        }
        if ([status isEqualToString:@"1"]) {
            [self.tableView.mj_header endRefreshing];
            NSError *error;
            PostListModel *postListModel = [[PostListModel alloc] initWithDictionary:dict[@"data"] error:&error];
            self.postListModel = postListModel;
            if (postListModel != nil) {
                self.dataArray = postListModel.postListDataModel;
            }
            [self.tableView reloadData];
            
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [self.tableView.mj_header endRefreshing];
        [HttpManager showFail];
    }];
}

-(void)requestMoreData
{
//    _page ++;
    NSString *url = [NSString stringWithFormat:@"%@app/notice_record",REQUESTHEADER];
    NSDictionary *dict = @{[NSNumber numberWithInteger:self.current_page]:@"page"};
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        NSDictionary * dict = successResponse;
        if (dict == nil) {
            return ;
        }
        if ([status isEqualToString:@"1"]) {
            [self.tableView.mj_footer endRefreshing];
            NSError *error;
            self.page = [successResponse[@"data"][@"last_page"] integerValue];
            // 成功
//            for (NSDictionary *dic in successResponse[@"data"][@"data"]) {
                PostListModel *postListModel = [[PostListModel alloc] initWithDictionary:dict[@"data"] error:&error];
                self.postListModel = postListModel;
                if (postListModel != nil) {
                    self.dataArray = self.dataArray = postListModel.postListDataModel;
//                }
            }
            [self.tableView reloadData];
            
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [self.tableView.mj_footer endRefreshing];
        [HttpManager showFail];
    }];
}

-(void)setTableViewUI
{
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    [self.view addSubview:self.tableView];
    
}

#pragma mark - tabelView的delegate 和datasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FPPostListTableViewCell * postListCell = [FPPostListTableViewCell cellWithTableView:tableView andIndexPath:indexPath];
    postListCell.postListDataModel = self.dataArray[indexPath.row];
    return postListCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    return view;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:true];

    FPPostDetailViewController *postDetailVC = [[FPPostDetailViewController alloc] init];
    postDetailVC.Id = [self.postListModel.postListDataModel valueForKey:@"Id"][indexPath.row];
    [self.navigationController pushViewController:postDetailVC animated:true];
    
}


@end


#pragma mark - 公告详情控制器
@interface FPPostDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)  BaseTableView      * tableDetailView;
@property (nonatomic,strong)  FPPostDetailModel  * postDetailModel;
@property (nonatomic,strong)  NSMutableArray     * dataArray;


@end

@implementation FPPostDetailViewController

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitle:@"公告详情"];
    [self setTableViewUI];
    
    [self requestData];
    
}

-(void)requestData
{
    NSString *url = [NSString stringWithFormat:@"%@app/notice_detail",REQUESTHEADER];
    NSDictionary *dict = @{@"id":self.Id};//[NSString stringWithFormat:@"%@"],
    
    WeakObj(self);
    [HttpManager postHttpRequestByPost:url andParameter:dict success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        NSDictionary * dict = successResponse;
        if ([status isEqualToString:@"1"]) {
            NSError *error;
            // 成功
            FPPostDetailModel *postDetailModel = [[FPPostDetailModel alloc] initWithDictionary:dict[@"data"] error:&error];
            selfWeak.postDetailModel = postDetailModel;
            if (postDetailModel != nil) {
                [selfWeak.dataArray addObject:postDetailModel];
            }
            [selfWeak.tableDetailView reloadData];
        }else{
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    }andFailure:^(id failureResponse) {
            [HttpManager showFail];
    }];
    
}

-(void)setTableViewUI
{
    self.tableDetailView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT)];
    self.tableDetailView.delegate = self;
    self.tableDetailView.dataSource = self;
    [self.view addSubview:self.tableDetailView];
}

#pragma mark - tabelView的delegate 和datasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    FPPostDetailTableViewCell *postDetailCell = [FPPostDetailTableViewCell cellWithTableView:tableView andIndexPath:indexPath];
    postDetailCell.postDetailModel = self.dataArray[indexPath.row];
    return postDetailCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:false];
}

@end

